﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlinePharmacyV001.Models;

namespace OnlinePharmacyV001.Controllers
{ 
    public class RoleController : Controller
    {
        private OnlinePharmacyEntities db = new OnlinePharmacyEntities();

        //
        // GET: /Role/

        public ViewResult Index()
        {
            return View(db.Roles.ToList());
        }

        //
        // GET: /Role/Details/5

        public ViewResult Details(int id)
        {
            Role role = db.Roles.Find(id);
            return View(role);
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            ViewBag.Permissions= db.Permissions.ToList(); 
            return View();
        } 

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(Role role, int[] per)
        {
            if (ModelState.IsValid)
            {                
                db.Roles.Add(role);
                for (int j = 0; j < per.Length; j++)
                {
                    role.Permissions.Add(db.Permissions.Find(per[j]));
                }
                //role.Permissions.Add(db.Permissions.Find(per));
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(role);
        }
        
        //
        // GET: /Role/Edit/5
 
        public ActionResult Edit(int id)
        {
            var role = db.Roles.Include(a=>a.Permissions).Where(b=>b.rlId==id);
            ViewBag.Pers = db.Permissions.ToList(); 
            return View(role.First());
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit(Role role, int[] perms)
        {
            if (ModelState.IsValid)
            {
                db.Entry(role).State = EntityState.Modified;
                for (int i = 0; i < perms.Length; i++ )
                    role.Permissions.Add(db.Permissions.Find(perms[i]));
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Role/Delete/5
 
        public ActionResult Delete(int id)
        {
            Role role = db.Roles.Find(id);
            return View(role);
        }

        //
        // POST: /Role/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Role role = db.Roles.Find(id);
            db.Roles.Remove(role);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}